package com.willwinder.ugs.nbm.visualizer.actions;

import com.willwinder.ugs.nbp.lib.lookup.CentralLookup;
import com.willwinder.universalgcodesender.model.BackendAPI;
import com.willwinder.universalgcodesender.utils.SwingHelpers;

import javax.swing.*;
import java.awt.event.ActionEvent;

public class OpenTestFileAction extends AbstractAction {

    private final BackendAPI backend;

    public OpenTestFileAction() {
        super("Open");
        this.backend = CentralLookup.getDefault().lookup(BackendAPI.class);
    }

    @Override
    public void actionPerformed(ActionEvent event) {
        String directory = backend.getSettings().getLastOpenedFilename();
        SwingHelpers.openFile(directory).ifPresent(file -> {
            try {
                backend.setGcodeFile(file);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        });

    }
}
